<template>
    <div
            v-loading="globalLoading"
            element-loading-text="正在添加..."
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.8)"
    >
        <el-card class="box-card">
            <div>
                <el-input placeholder="角色英文名" v-model="role.name" size="small" style="width: 40%">
                    <template slot="prepend">ROLE_</template>
                </el-input>
                <el-input placeholder="角色中文名" v-model="role.nameZh" size="small" style="width: 30%;margin-left: 2%" @keydown.enter.native="doAddRole">
                </el-input>
                <el-button type="primary" size="small" style="margin-left: 3%" @click="doAddRole">添加角色</el-button>
            </div>
            <div style="margin-top: 20px">
                <el-collapse v-model="activeName"
                             v-loading="loading"
                             element-loading-text="正在加载..."
                             element-loading-spinner="el-icon-loading"
                             element-loading-background="rgba(0, 0, 0, 0.8)"
                             accordion
                             @change="change">
                    <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
                        <el-card>
                            <div slot="header" class="clearfix" style="width: 600px">
                                <span>可访问资源</span>
                                <el-button style="float: right; padding: 3px 0;margin-right: 40px;color: #f00" icon="el-icon-delete" @click="deleteRole(r)"></el-button>
                            </div>
                            <div class="text item">
                                <el-tree
                                        :data="allMenus"
                                        :props="defaultProps"
                                        show-checkbox
                                        :default-checked-keys="selectMenus"
                                        node-key="id"
                                        ref="tree"
                                       >
                                </el-tree>
                                <div style="display: flex;justify-content: flex-end;margin-top: 20px">
                                    <el-button @click="cancelUpdate">取消修改</el-button>
                                    <el-button type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
                                </div>
                            </div>
                        </el-card>
                    </el-collapse-item>
                </el-collapse>
            </div>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "PermissManager",
        data(){
            return{
                activeName:'1',
                role:{
                    nameZh:'',
                    name:''
                },
                roles:[],
                selectMenus:[],
                defaultProps:{
                    label: 'name',
                    children: 'children'
                },
                allMenus:[],
                globalLoading:false,
                loading:false
            }
        },
        methods:{
            doAddRole() {
                if (this.role.name && this.role.nameZh) {
                    this.globalLoading = true;
                    this.postRequest("/system/basic/per/role", this.role).then(resp => {
                        this.globalLoading = false;
                        if (resp) {
                            this.role.name = '';
                            this.role.nameZh = '';
                            this.initRoles();
                        }
                    })
                } else {
                    this.$message.error('数据不可以为空');
                }
            },
            deleteRole(role) {
                this.$confirm('此操作将永久删除【' + role.nameZh + '】角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/per/role/" + role.id).then(resp => {
                        if (resp) {
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doUpdate(rid, index) {
                console.log(rid,index);
                let tree = this.$refs.tree[index];
                let selectedKeys = tree.getCheckedKeys(true);
                let url = '/system/basic/per/?rid=' + rid;
                selectedKeys.forEach(key => {
                    url += '&mids=' + key;
                })
                this.putRequest(url).then(resp => {
                    if (resp) {
                        this.activeName = -1;
                    }
                })
            },
            cancelUpdate() {
                this.activeName = -1;
            },
            change(rid){
                if (rid){
                    this.initAllMenus();
                    this.initSelectMenus(rid);
                }
            },
            initRoles(){
                this.getRequest("/system/basic/role/").then(res =>{
                    this.roles = res.data;
                })
            },
            initAllMenus(){
                this.getRequest("/system/basic/per/").then(res =>{
                    this.allMenus = res.data;
                })
            },
            initSelectMenus(rid){
                this.getRequest("/system/basic/per/mids/"+rid).then(res =>{
                    this.selectMenus = res.data;
                })
            }
        },
        mounted() {
            this.initRoles();
        }
    }
</script>

<style scoped>

</style>